package exam.hadoop;

import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
/*    key：表示当前正在处理的日期。
    values：包含了所有与当前键（日期）相关联的温度值。
    context：用于收集Reducer的输出。*/


public class     TemperatureReducer extends Reducer<Text, FloatWritable, Text, FloatWritable> {

    public void reduce(Text key, Iterable<FloatWritable> values, Context context)

            throws IOException, InterruptedException {
        float sum = 0;
        int count = 0;
                /*sum和count来分别记录温度值的总和和数量*/
        for (FloatWritable val : values) {
            sum += val.get();
            count++;
        }
        if (count > 0) {
            float average = sum / count;
            context.write(key, new FloatWritable(average));
        }
    }
}

